package com.example.bean;

import java.sql.*;
import java.util.regex.Pattern;


public class RegisterBean {
    private String username;
    private String password;
    private String confirmPassword;
    private String message;
    private  boolean registerResult;

    public RegisterBean() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean getRegisterResult() {
        return registerResult;
    }

    public void setRegisterResult(boolean registerResult) {
        this.registerResult = registerResult;
    }

    // 执行注册逻辑
    public void register() {

        if (username == null || username.isEmpty() || password == null || password.isEmpty() || confirmPassword == null || confirmPassword.isEmpty() )
        {
            this.message = "注册失败：请输入账号或密码!";
            this.registerResult=false;
            return;
        }

        // 使用正则表达式验证用户名
        if (!Pattern.matches("^[a-zA-Z0-9]+$", username)) {
            this.message = "注册失败：用户名只能包含英文字母和数字！";
            this.registerResult = false;
            return;
        }

        if (!password.equals(confirmPassword)) {
            this.message = "注册失败：两次密码不一致!";
            this.registerResult=false;
        } else {
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/uerinfo?useSSL=false&allowPublicKeyRetrieval=true";
                String user = "root";
                String pass = "123456";

                connection = DriverManager.getConnection(url, user, pass);
                //1. 检查用户名是否存在
                String selectSql = "SELECT * from userinfo where username = ? ";
                statement = connection.prepareStatement(selectSql);
                statement.setString(1, username);
                resultSet = statement.executeQuery();
                if (resultSet.next()) {
                    this.message = "注册失败：用户名已存在!";
                    this.registerResult=false;
                } else {  //不存在， 进行注册


                    String insertSql = "INSERT into userinfo (username,password) VALUES (?,?)";
                    statement = connection.prepareStatement(insertSql);
                    statement.setString(1, username);
                    statement.setString(2, password);
                    int insertCount = statement.executeUpdate();
                    if (insertCount > 0) {
                        this.message = "注册成功！";
                        this.registerResult=true;
                    } else {
                        this.message = "注册失败！请重新注册";
                        this.registerResult=false;
                    }
                }
                if(resultSet != null) resultSet.close();
                if (statement!= null) statement.close();
            } catch (ClassNotFoundException e) {
                this.message = "注册失败:数据库驱动加载失败";
                this.registerResult=false;
            } catch (SQLException e) {
                this.message = "注册失败:数据库执行异常";
                this.registerResult=false;
            } finally {
                try {
                    if (connection != null) connection.close();
                } catch (SQLException ex) {
                    this.message = "注册失败:资源关闭异常" ;
                    this.registerResult=false;
                }
            }
        }
    }
}